home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11485 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  81 lines

  1. Path: EU.net!sun4nl!ittpub!ittpub!nntp
  2. Newsgroups: comp.lang.c++
  3. Subject: Re: Lifetime of temporary parameter objects
  4. Message-ID: <1996Mar14.131613.1805@ittpub>
  5. From: wil@ittpub.nl (Wil Evers)
  6. Date: 14 Mar 96 13:16:13 WET
  7. References: <4i6607$l4q@damon.irf.uni-dortmund.de>
  8. Distribution: world
  9. Nntp-Posting-Host: lintilla
  10.  
  11. In article <4i6607$l4q@damon.irf.uni-dortmund.de>  
  12. rothert@damon.irf.uni-dortmund.de (Bernd Rothert) writes:
  13.  
  14. > I need some advice concerning the lifetime of temporary parameter
  15. > objects infunction calls:
  16. > It is obviously correct to pass a reference to a temporary object to a
  17. > function - the compiler ensures that the temporary stays alive as long
  18. > as the reference exists (actual parameter of "const Stars&").
  19. > What if passing a pointer to the CONTENTS of an explicitly constructed
  20. > temporary:
  21. > ---
  22. > #include <string.h>
  23. > #include <iostream.h>
  24. > class Stars {
  25. >   public:
  26. >     Stars(int n) : _s(new char[n+1]) {
  27. >         memset(_s, '*', n);
  28. >         _s[n] = '\0';
  29. >     }
  30. >     ~Stars() {
  31. >         delete _s;
  32.         ^--- that should be delete[] _s;
  33. >         _s = 0;
  34. >     }
  35. >     const char* get() const { return _s; }
  36. >   private:
  37. >     char* _s;
  38. > };
  39. > void banner(const Stars& st, const char* s) {
  40. >                //=============
  41. >     cout << st.get() << endl << s << endl;
  42. > }
  43. > int main() {
  44. >     banner(40, Stars(40).get());
  45. >          //===============
  46. >     return 0;
  47. > }
  48. > ---
  49. > The program passes a pointer to the "_s" member of a "Stars" object
  50. > to the "banner" function. This works for every compiler I have seen but
  51. > I don't know if I can rely on this behaviour and prefer defining an
  52. > auxiliary variable which is guaranteed to live until the function call
  53. > returns.
  54. > Can anyone give me a hint?
  55.  
  56. The older rules as defined in the ARM (1990) allowed the temporary Stars  
  57. object created in `Stars(40).get()' to be destroyed *before* control was  
  58. passed to banner(). Older versions of GNU C++ are known to do this. The  
  59. draft ANSI/ISO standard effectively says the temporary will be destructed  
  60. at the end of the statement, that is, after the call to banner(). But note  
  61. that even the new rules may surprise us:
  62.  
  63. void f()
  64. {
  65.     char *p = Stars(40).get();
  66.     cout << p << endl;    // has undefined result
  67. }
  68.  
  69. So if you have a class that passes a pointer to some data in its internal  
  70. representation, I would always recommend using an explictly named  
  71. variable.
  72.  
  73. Hope this helps,
  74.  
  75. - Wil
  76.